#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-01-25 20:53:59

import gevent
import datetime
import re


def task(pid):
    gevent.sleep(1)
    print('task %s done' % pid)


def getLocalTime(*args):
    format_dict = {
        '年': '%Y',
        '月': '%m',
        '日': '%d',
        '时': '%H',
        '分': '%M',
        '秒': '%S',
        '周': '%w',
    }

    if len(args) == 0:
        return datetime.datetime.now().strftime('%Y-%m-%d %H:%m:%S')
    else:
        format_string = args
        for s in format_dict:
            reobj = re.compile(s)
            format_string = reobj.sub(format_dict[s], format_string)
        return datetime.datetime.now().strftime(format_string)


def async():
    threads = [gevent.spawn(task, i) for i in range(10)]
    gevent.joinall(threads)


def sync():
    for i in range(10):
        task(i)


print('sync:')
print('start:%s' % getLocalTime())
sync()
print('end:%s' % getLocalTime())

print('async:')
print('start:%s' % getLocalTime())
async()
print('end:%s' % getLocalTime())
